package cn.siat.gene.controller;

import cn.siat.common.core.web.controller.BaseController;
import cn.siat.common.core.web.domain.AjaxResult;
import cn.siat.common.log.annotation.Log;
import cn.siat.common.log.enums.BusinessType;
import cn.siat.common.security.annotation.RequiresPermissions;
import cn.siat.gene.domain.Template;
import cn.siat.gene.service.TemplateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/template")
public class TemplateController extends BaseController {
    @Autowired
    private TemplateService templateService;

    @Log(title = "模板保存", businessType = BusinessType.INSERT)
    @ApiOperation(value = "模板保存", notes = "模板保存")
    @RequiresPermissions("programme:programme:template")
    @PostMapping(value = "/saveTemplate")
    public AjaxResult saveTemplate(@RequestBody Template template) {
        templateService.saveTemplate(template);
        return AjaxResult.success("保存成功");
    }

    @DeleteMapping(value = "/deleteTemplate")
    public AjaxResult deleteTemplate(String id) {
        templateService.deleteTemplate(id);
        return AjaxResult.success("删除成功");
    }

//    @Log(title = "模板查询", businessType = BusinessType.OTHER)
    @ApiOperation(value = "模板查询", notes = "模板查询")
    @GetMapping(value = "/getTemplateList")
    public List<Template> getTemplateList(String name) {
        List<Template> templateList= templateService.getTemplateList(name);
        return templateList;
    }

    /**
     * 校验名称(已存在返回true)
     * @return
     */
    @GetMapping("/checkName")
    public AjaxResult checkName(String name,String id){
        Boolean isExist = templateService.checkName(name,id);
        return AjaxResult.success(isExist);
    }

    /**
     * 判断方案是否是浏览视图
     */
    @GetMapping("/checkIsBrowsingMode/{id}")
    public AjaxResult checkIsBrowsingMode(@PathVariable String id){
        //0 文件不存在，1 浏览视图
        Integer isBrowsingMode = templateService.checkIsBrowsingMode(id);
        return AjaxResult.success(isBrowsingMode);
    }
}
